package com.jmyd.sharding.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.jmyd.sharding.base.dto.KeywordQueryDto;
import com.jmyd.sharding.base.service.impl.BaseServiceImpl;
import com.jmyd.sharding.base.vo.PageBean;
import com.jmyd.sharding.domain.DomainPoFactory;
import com.jmyd.sharding.domain.dto.SysUserDto;
import com.jmyd.sharding.domain.dto.SysUserUpdateDto;
import com.jmyd.sharding.domain.po.SysCoupon;
import com.jmyd.sharding.domain.po.SysUser;
import com.jmyd.sharding.domain.vo.SysCouponVo;
import com.jmyd.sharding.domain.vo.SysUserVo;
import com.jmyd.sharding.mapper.SysCouponMapper;
import com.jmyd.sharding.mapper.SysUserMapper;
import com.jmyd.sharding.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author samphin
 * @date 2024/6/18 10:46
 */
@Service
@Slf4j
public class SysUserService extends BaseServiceImpl<Long, SysUserDto, SysUser> implements ISysUserService {
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysCouponMapper sysCouponMapper;

    /**
     * 保存数据
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(SysUserDto dto) {
        SysUser sysUser = DomainPoFactory.newInstance(SysUser.class, null).build(dto);
        sysUser.setDeleted(false);
        this.sysUserMapper.insertSelective(sysUser);

        SysCoupon sysCoupon = DomainPoFactory.newInstance(SysCoupon.class, null);
        sysCoupon.setName(dto.getRealName() + "的优惠券.");
        sysCoupon.setUserId(sysUser.getId());
        final Date date = new Date();
        sysCoupon.setValidStartTime(date);
        sysCoupon.setValidEndTime(DateUtils.addDays(date, 30));
        sysCoupon.setCode(RandomStringUtils.random(8, true, true));
        sysCoupon.setCreateTs(date);
        sysCoupon.setUpdateTs(date);
        sysCoupon.setExclusiveStatus(true);
        sysCoupon.setDiscountAmount(BigDecimal.valueOf(0.2));
        sysCoupon.setStatus(0);
        sysCoupon.setDeleted(false);
        this.sysCouponMapper.insertSelective(sysCoupon);
        return true;
    }

    @Override
    public boolean updateById(SysUserUpdateDto dto) {
        SysUser sysUser = new SysUser().build(dto);
        this.sysUserMapper.updateById(sysUser);
        return true;
    }

    @Override
    public boolean deleteById(Long id) {
        this.sysUserMapper.deleteById(id);
        return true;
    }

    @Override
    public PageBean<SysUserVo> paging(KeywordQueryDto queryDto) {
        final Page<Object> page = PageMethod.startPage(queryDto.getPageNum(), queryDto.getPageSize());
        final List<SysUserVo> voList = this.sysUserMapper.paging(queryDto);
        return new PageBean<>(voList, page);
    }

    @Override
    public SysUserVo queryDetail(Long id) {
        final SysUser sysUser = this.sysUserMapper.selectById(id);
        final List<SysCouponVo> couponList = this.sysCouponMapper.selectUserCouponList(id);
        return new SysUserVo().buildVo(sysUser, couponList);
    }
}